package org.objective.ddl.type;

import org.objective.ddl.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 *
 * @author gus
 */
public abstract class DataType
{
    @DBTypeAttribute(representType = long.class, domain="DOM_REFERENCE", reference = true)
    public final static long Reference = 1;
    
    @DBTypeAttribute(representType = String.class, domain="DOM_STRING", size = 256)
    public final static long String = 3;
    
    @DBTypeAttribute(representType = long.class, domain="DOM_INTEGER")
    public final static long Integer = 5;
    
    @DBTypeAttribute(representType = double.class, domain="DOM_DECIMAL")
    public final static long Decimal = 7;
    
    @DBTypeAttribute(representType = Date.class, domain="DOM_DATATIME")
    public final static long DateTime = 11;
    
    @DBTypeAttribute(representType = UUID.class, domain="DOM_UUID")
    public final static long UUID = 13;

    @DBTypeAttribute(representType = BlobWraper.class, domain="DOM_BLOB")
    public final static long Blob = 17;

    @DBTypeAttribute(representType = Spatial.class, domain="DOM_SPATIAL")
    public final static long Spatial = 19;



    public static boolean isDataType( long id )
    {
        return annotationMap.containsKey(id);
    }

    public static Class repClass( long type )
    {
        return annotationMap.get(type).representType();
    }

    public static String getDomain(long type)
    {
        return annotationMap.get(type).domain();
    }

    private final static Map<Long, DBTypeAttribute> annotationMap = new HashMap<Long, DBTypeAttribute>();

    static
    {
        if ( annotationMap.isEmpty() )
        {
            for ( Field field : DataType.class.getDeclaredFields() )
            {
                if ( Modifier.isStatic(field.getModifiers()) )
                {
                    if ( field.isAnnotationPresent(DBTypeAttribute.class) )
                    {
                        DBTypeAttribute attribute = field.getAnnotation(DBTypeAttribute.class);
                        try
                        {
                            annotationMap.put(field.getLong(null), attribute);
                        } catch (Exception e)
                        {}
                    }
                }
            }
        }
    }
}


